﻿#include "xfunction.h"

XFunction::XFunction(){};

XFunction::XFunction(XCoil *coil)
    :xCoilSingle(coil)
{

}

XFunction::XFunction(XCoil *coilA, XCoil *coilB)
    : xCoilSource(coilA),
      xCoilDestination(coilB)
{

}

XFunction::XFunction(XRegister *registerA, double value)
    : xRegisterSingle(registerA),
      setValue(value)
{

}

XFunction::XFunction(XRegister *registerA, XRegister *registerB)
    : xRegisterSource(registerA),
      xRegisterDestination(registerB)
{

}

XFunction::XFunction(QList<XRegister *> listA, QList<XRegister *> listB)
    : xRegisterListSource(listA),
      xRegisterListDestination(listB)
{

}

XFunction::XFunction(XPushButtonFourArithmetic *pushButtonFourArithmetic)
    : pushButtonFourArithmetic(pushButtonFourArithmetic)
{

}

XFunction::~XFunction()
{
    xCoilSingle = nullptr;
    xCoilSource = nullptr;
    xCoilDestination = nullptr;
    xRegisterSingle = nullptr;
    xRegisterSource = nullptr;
    xRegisterDestination = nullptr;
    pushButtonFourArithmetic = nullptr;
}

/*!
 * \brief XFunction::setCoil
 * \param coil
 */
void XFunction::setCoil()
{
    if(!xCoilSingle)
        return;
qDebug() << "setCoil";
    xCoilSingle->setCoilValue(true);
}

/*!
 * \brief XFunction::resetCoil
 * \param coil
 */
void XFunction::resetCoil()
{
    if(!xCoilSingle)
        return;
qDebug() << "---------resetCoil";
    xCoilSingle->setCoilValue(false);
}

/*!
 * \brief XFunction::reverseCoil
 * \param coil
 */
void XFunction::reverseCoil()
{
    if(!xCoilSingle)
        return;
qDebug() << "--------------------reverseCoil";
    (true == xCoilSingle->getCoilValue())
        ? (xCoilSingle->setCoilValue(false))
        : (xCoilSingle->setCoilValue(true));
}

/*!
 * \brief XFunction::copyCoil
 * \param coilA
 * \param coilB
 */
void XFunction::copyCoil()
{
    if(!xCoilSource)
        return;
    if(!xCoilDestination)
        return;
qDebug() << "---------------------------------copyCoil";
    xCoilSource->setCoilValue(xCoilDestination->getCoilValue());
}

/*!
 * \brief XFunction::setRegister
 */
void XFunction::setRegister()
{
    if(!xRegisterSingle)
        return;
//qDebug() << "-----------------------------------------setRegister";
    xRegisterSingle->setRegisterValue(setValue);
}

/*!
 * \brief XFunction::copyRegister
 */
void XFunction::copyRegister()
{
    if(!xRegisterSource)
        return;
    if(!xRegisterDestination)
        return;
//qDebug() << "----------------------------------------------------copyRegister";
    xRegisterSource->setRegisterValue(xRegisterDestination->getRegisterValue());
}

/*!
 * \brief XFunction::copyRegisterBlock
 */
void XFunction::copyRegisterBlock()
{
qDebug() << "----------------------------------------------------------copyRegisterBlock";
    //判断两个寄存器组是否为空
    if((!xRegisterListSource.size()) || (!xRegisterListDestination.size()))
        return;

    //判断两个寄存器组长度是否相同
    if(xRegisterListSource.size() != xRegisterListDestination.size())
        return;

    //遍历两个寄存器组，进行寄存器块传送
    QList<XRegister*>::iterator i = xRegisterListSource.begin();
    QList<XRegister*>::iterator j = xRegisterListDestination.begin();
    while (i != xRegisterListSource.end())
    {
        (*j)->setRegisterValue((*i)->getRegisterValue());
        ++i;
        ++j;
    }
}

/*!
 * \brief XFunction::fourArithmetic
 */
void XFunction::fourArithmetic()
{
qDebug() << "--------------------------------------------------------------------------fourArithmetic";
    double leftOperand;
    double rightOperand;
    double result;

    //分别获得左操作数与右操作数
    (Constant == pushButtonFourArithmetic->getLeftOperandNumberType())
            ?(leftOperand = pushButtonFourArithmetic->getLeftOperandConstant())
            :(leftOperand = pushButtonFourArithmetic->getLeftOperandRegister()->getRegisterValue());

    (Constant == pushButtonFourArithmetic->getRightOperandNumberType())
            ?(rightOperand = pushButtonFourArithmetic->getRightOperandConstant())
            :(rightOperand = pushButtonFourArithmetic->getRightOperandRegister()->getRegisterValue());

    //计算结果
    switch (pushButtonFourArithmetic->getArithmeticType())
    {
    case Addition:
    {
        result = leftOperand + rightOperand;
        break;
    }
    case Subtraction:
    {
        result = leftOperand - rightOperand;
        break;
    }
    case Multiplication:
    {
        result = leftOperand * rightOperand;
        break;
    }
    case Division:
    {
        result = leftOperand / rightOperand;
        break;
    }
    }

    //判断上限
    if(pushButtonFourArithmetic->getUpperLimitEnable())
    {
        //声明上限
        int upperLimit;
        //判断上限的类别
        (Constant == pushButtonFourArithmetic->getUpperLimitType())
                ?(upperLimit = pushButtonFourArithmetic->getUpperLimitConstant())
                :(upperLimit = pushButtonFourArithmetic->getUpperLimitRegister()->getRegisterValue());
        //判断结果是否越界，并调整
        if(upperLimit < result)
            result = upperLimit;
    }
    //判断下限
    if(pushButtonFourArithmetic->getLowerLimitEnable())
    {
        //声明下限
        int lowerLimit;
        //判断下限的类别
        (Constant == pushButtonFourArithmetic->getLowerLimitType())
                ?(lowerLimit = pushButtonFourArithmetic->getLowerLimitConstant())
                :(lowerLimit = pushButtonFourArithmetic->getLowerLimitRegister()->getRegisterValue());
        //判断结果是否越界，并调整
        if(lowerLimit > result)
            result = lowerLimit;
    }

    //写入运算结果
    pushButtonFourArithmetic->getResultRegister()->setRegisterValue(result);
}
